package web.servlet;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

public class RegisterServlet extends HttpServlet {

    private String isLogin;
    private String username;
    private String password;
    private String repeatPassword;
    private String email;
    private String firstName;
    private String lastName;
    private String status;
    private String address1;
    private String address2;
    private String city;
    private String state;
    private String zip;
    private String country;
    private String phone;
    private String favouriteCategoryId;
    private String languagePreference;
    private String listOption;
    private String bannerOption;
    private String bannerName;
    private boolean is_admin;

    private String registerMsg;

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.username = req.getParameter("username");
        this.password = req.getParameter("password");
        this.repeatPassword = req.getParameter("repeatPassword");
        this.email = req.getParameter("email");
        this.firstName = req.getParameter("firstName");
        this.lastName = req.getParameter("lastName");
        this.status = req.getParameter("status");
        this.address1 = req.getParameter("address1");
        this.address2 = req.getParameter("address2");
        this.city = req.getParameter("city");
        this.state = req.getParameter("state");
        this.zip = req.getParameter("zip");
        this.country = req.getParameter("country");
        this.phone = req.getParameter("phone");
        this.favouriteCategoryId = req.getParameter("favouriteCategoryId");
        this.languagePreference = req.getParameter("languagePreference");
        this.listOption = req.getParameter("listOption");
        this.bannerOption = req.getParameter("bannerOption");

        if(!this.validate()){
            req.setAttribute("registerMsg",this.registerMsg);
            req.getRequestDispatcher("registerForm").forward(req, resp);
        }


        HttpSession session = req.getSession();


    }
    private boolean validate(){
        if(this.username !=null ){
            this.registerMsg = "Username is required";
            return false;
        }
        if(this.password !=null){
            this.registerMsg = "Password is required";
            return false;
        }
        if(this.repeatPassword !=null){
            this.registerMsg = "Password is required";
            return false;
        }
        if(this.repeatPassword!=this.password){
            this.registerMsg = "The passwords are inconsistent";
            return false;
        }
        if(this.email !=null){
            this.registerMsg = "Email is required";
            return false;
        }
        if(this.firstName !=null){
            this.registerMsg = "First Name is required";
            return false;
        }
        if(this.lastName !=null){
            this.registerMsg = "Last Name is required";
        }
        if(this.status !=null){
            this.registerMsg = "Status is required";
        }
        if(this.address1 !=null){
            this.registerMsg = "Address1 is required";
        }
        if(this.address2 !=null){
            this.registerMsg = "Address2 is required";
        }
        if(this.city !=null){
            this.registerMsg = "City is required";
        }
        if(this.state !=null){
            this.registerMsg = "State is required";
        }
        if(this.zip !=null){
            this.registerMsg = "Zip is required";
        }
        if(this.country !=null){
            this.registerMsg = "Country is required";
        }
        if(this.phone !=null){
            this.registerMsg = "Phone is required";
        }
        return true;
    }
}
